package com.tre.jdevtemplateboot.common.authority;

import com.tre.jdevtemplateboot.exception.business.SysUnAuthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author JDev
 */
@Component
public class AuthorizedPerson {

    @Autowired
    private TokenTakeApart tokenTakeApart;
    private String currentUserId;
    private String headerTokenUserId;

    public AuthorizedPerson scan(){
        this.headerTokenUserId = tokenTakeApart.takeDecryptedUserId();
        return this;
    }

    public AuthorizedPerson find(String currentUserId){
        this.currentUserId = currentUserId;
        return this;
    }

    public boolean any(){
        return !StringUtils.hasLength(currentUserId) || !currentUserId.equals(headerTokenUserId);
    }

    public  void  agreed(){
        if(!StringUtils.hasLength(currentUserId) || !currentUserId.equals(headerTokenUserId)){
            throw new SysUnAuthorizedException();
        }
    }
}
